#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
数据库重置脚本
强制删除并重新创建所有数据库表
"""
from app import app, db
from sqlalchemy import text

def reset_database():
    """重置数据库"""
    with app.app_context():
        try:
            print("正在重置数据库...")
            
            # 获取数据库连接
            with db.engine.connect() as conn:
                # 禁用外键约束检查
                conn.execute(text('SET FOREIGN_KEY_CHECKS = 0'))
                conn.commit()
                
                print("删除所有表...")
                db.drop_all()
                
                # 启用外键约束检查
                conn.execute(text('SET FOREIGN_KEY_CHECKS = 1'))
                conn.commit()
            
            print("重新创建所有表...")
            db.create_all()
            
            print("数据库重置完成！")
            
            # 检查表结构
            print("检查表结构...")
            inspector = db.inspect(db.engine)
            tables = inspector.get_table_names()
            print(f"已创建的表: {tables}")
            
            # 检查house表的字段
            if 'house' in tables:
                house_columns = inspector.get_columns('house')
                print("House表字段:")
                for col in house_columns:
                    print(f"  {col['name']}: {col['type']}")
            
        except Exception as e:
            print(f"重置数据库失败: {str(e)}")
            import traceback
            traceback.print_exc()

if __name__ == '__main__':
    reset_database() 